<layout:basic/>

<div id="my-calendar"></div>
<link rel="stylesheet" href="__PUBLIC__/css/fullcalendar.min.css"/>
<link rel="stylesheet" href="__PUBLIC__/css/fullcalendar.print.css" media='print'/>
<script src="__PUBLIC__/js/moment.js"></script>
<script src="__PUBLIC__/js/fullcalendar.min.js"></script>

<script>
    $(document).ready(function () {
        var calendar = $('#my-calendar');
        $.get("__PUBLIC__/admin/calendar/getlist", function (data) {
            calendar.fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
                dayNamesShort: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
                today: ["今天"],
                firstDay: 1,
                buttonText: {
                    today: '本月',
                    month: '月',
                    week: '周',
                    day: '日',
                    prev: '上一月',
                    next: '下一月'
                },
                defaultDate: data.now,
                lang: 'zh-cn',
                navLinks: true, // can click day/week names to navigate views
                selectable: true,
                selectHelper: true,
                select: function (start, end) {
                    var title = prompt('填写你的记录的:');
                    if (title) {
                        /** 开始和结束时间戳 */
                        var start_time = start.unix();
                        var end_time = end.unix();
                        console.log(start_time, end_time);
                        $.get("__PUBLIC__/admin/calendar/add", {
                            title: title,
                            start: Date.parse(start.toString()) / 1000,
                            end: Date.parse(end.toString()) / 1000
                        }, function (data) {
                            if (data.status) {
                                calendar.fullCalendar('renderEvent', {
                                    id: data.data.id,
                                    title: title,
                                    start: start,
                                    end: end
                                }, true); // stick? = true
                            } else {
                                alert('添加失败')
                            }
                            calendar.fullCalendar('unselect');
                        });
                    } else {
                        calendar.fullCalendar('unselect');
                    }
                },
                eventClick: function (event) {

                },
                eventDragStop: function (event) {
                    /* 拖动后删除*/
                    console.log(event);
                    if (confirm("请确认删除‘" + event.title + "’？")) {
                        $.get("__PUBLIC__/admin/calendar/rm", {
                            id: event.id
                        }, function (data) {
                            if (data.status) {
                                calendar.fullCalendar('removeEvents', event.id);
                            } else {
                                alert('删除失败')
                            }
                        });
                    }
                },
                editable: true,
                eventLimit: true,
                // allow "more" link when too many events
                //              eventClick: function (event) {
                //                  console.log(event);
                //              },
                events: data.data
            });
        });

    });
</script>